package com.miplus.ccrm.core.service.conf;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.query.MPJQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.EnumRCode;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.common.ServiceException;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.conf.WfNextStepVo;
import com.miplus.ccrm.core.controller.vo.conf.WfStepEditReqVo;
import com.miplus.ccrm.core.controller.vo.conf.WfStepReqVo;
import com.miplus.ccrm.core.controller.vo.conf.WfStepResVo;
import com.miplus.ccrm.core.pojo.MsgDefine;
import com.miplus.ccrm.core.pojo.WfActions;
import com.miplus.ccrm.core.pojo.WfSteps;
import com.miplus.ccrm.core.mapper.WfStepsMapper;
import com.miplus.ccrm.core.pojo.WfWorkflow;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author pc
* @description 针对表【wf_steps】的数据库操作Service实现
* @createDate 2024-03-09 17:38:02
*/
@Service
public class WfStepsServiceImpl extends MPJBaseServiceImpl<WfStepsMapper, WfSteps>
    implements WfStepsService{

    @Override
    public IPage<WfStepResVo> wfStepList(WfStepReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                WfStepResVo.class,
                new MPJLambdaWrapper<WfSteps>()
                        .leftJoin(WfWorkflow.class,"wf",WfWorkflow::getWfid,WfSteps::getWfid)
                        .selectAs("fun_getnames('WF.STEP.TYPE',t.type)",WfStepResVo::getTypedsc)
                        .selectAs("(select dsc from sys_names n where n.tid=wf.formid and n.id=t.status)",WfStepResVo::getStatusdsc)
                        .selectAll(WfSteps.class)
                        .eq("t.wfid",paras.getWfid())
                        .like(JPubUtils.IsNotEmpty(paras.getName()),"name",paras.getName())
                        .orderByAsc(WfSteps::getStepid)
        );
    }

    @Override
    public List<LabelResVo> getStepIdList(Integer wfid)
    {
        return selectJoinList(
                LabelResVo.class,
                new MPJLambdaWrapper<WfSteps>()
                        .selectAs("stepid",LabelResVo::getValue)
                        .selectAs("name",LabelResVo::getLabel)
                        .select("stepid","name")
                        .eq("wfid",wfid)
        );
    }

    @Override
    public CmdResVo updateStep(WfStepEditReqVo paras)
    {
        WfSteps def=new WfSteps();
        BeanUtils.copyProperties(paras,def);
        int ret= baseMapper.updateById(def);
        return new CmdResVo(ret,def.getStepid().toString());
    }

    @Override
    public CmdResVo insertStep(WfStepEditReqVo paras)
    {
        if(JPubUtils.IsEqual(paras.getType(),"1")){//表示开始
            WfSteps wfSteps=getFirstStepInfo(paras.getWfid());
            if(wfSteps!=null){
                throw new ServiceException(EnumRCode.WfStartStepOne.value, EnumRCode.WfStartStepOne.name);
            }
        }
        WfSteps def=new WfSteps();
        BeanUtils.copyProperties(paras,def);
        int ret= baseMapper.insert(def);
        return new CmdResVo(ret,def.getStepid().toString());
    }

    @Override
    public CmdResVo deleteStep(String id)
    {
        int ret= baseMapper.deleteById(JPubUtils.StringToInt(id));
        return new CmdResVo(ret,id);
    }

    @Override
    public WfSteps getFirstStepInfo(Integer wfid)
    {
        //后续只允许返回1行控制
        return baseMapper.selectJoinOne(
                WfSteps.class,
                new MPJLambdaWrapper<WfSteps>()
                        .selectAll(WfSteps.class)
                        .eq(WfSteps::getWfid,wfid)
                        .eq(WfSteps::getType,"1")
        );
    }

    @Override
    public WfNextStepVo getStepInfoByAction(Integer actionid)
    {
        return selectJoinOne(WfNextStepVo.class,
                new MPJLambdaWrapper<WfSteps>()
                        .innerJoin(WfActions.class,WfActions::getNextstepid,WfSteps::getStepid)
                        .select(WfActions::getActionid,WfActions::getNextstepid)
                        .select(WfSteps::getType,WfSteps::getStatus)
                        .eq(WfActions::getActionid,actionid)
        );
    }
}




